﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectWebNhac.BUS;
using System.Data;
using ProjectWebNhac.DTO;
using System.IO;

namespace ProjectWebNhac.GUI.User
{
    public partial class TrangChu : System.Web.UI.Page
    {
        AlbumBus abbus = new AlbumBus();
        CaSiBus csbus = new CaSiBus();
        BaiHatBus bhbus = new BaiHatBus();
        VideoBus videoBus = new VideoBus();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //loadAlbumhot();
                //loadCaSiVietNamTop15();
                //loadCaSiAuMyTop15();
                //loadCaSiHanQuocTop15();
                //loadBaiHatMoi();
                //loadBXHTop20();
                LoadBaiHatNoiBat();
                LoadAlbumHot();
                LoadAlbumMoiNhat();
                LoadVideoMoiNhat();
                loadBXHTop20();
                LoadBXHVideo();
                loadPlaylist();
                if (Session["ThanhVien"] != null)
                {
                    NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                    txtUserName.Text = objUser.TaiKhoan;
                }
            }
        }


        private void loadPlaylist()
        {
            if (Session["ThanhVien"] != null)
            {
                NguoiDungDTO objUser = (NguoiDungDTO)Session["ThanhVien"];
                PlaylistBus plBus = new PlaylistBus();
                dropPlaylist.DataSource = plBus.LayPlaylistTheoTaiKhoan(objUser.TaiKhoan);
                dropPlaylist.DataTextField = "TenPlaylist";
                dropPlaylist.DataValueField = "MaPlaylist";
                dropPlaylist.DataBind();
            }
        }

        public static bool AddPlayList(string MaPL, string MaBaiHat)
        {
            PlaylistBus plBus = new PlaylistBus();

            return plBus.ThemBaiHatVaoPlayList(Convert.ToInt16(MaPL)
                , Convert.ToInt16(MaBaiHat));

        }

        public static string CreateNewPL(string tenPlaylist, string taikhoan)
        {
            PlaylistBus plBus = new PlaylistBus();
            bool isSuccess = true;
            foreach (DataRow row in plBus.LayPlaylistTheoTaiKhoan(taikhoan).Rows)
            {
                string oldName = row["TenPlaylist"].ToString();
                oldName = oldName.TrimEnd();
                oldName = oldName.ToLower();
                if (oldName.Equals(tenPlaylist.ToLower().TrimEnd()))
                {
                    isSuccess = false;
                }
            }
            if (isSuccess)
            {
                isSuccess = plBus.Taomoiplaylist(taikhoan, tenPlaylist);
            }

            if (isSuccess)
            {
                return plBus.GetMaxIDPlaylist();
            }
            else
            {
                return "failed";
            }
        }

        //private void loadAlbumhot()
        //{
        //    reAlbumHot.DataSource = abbus.LayDanhSachAlbumHot();
        //    reAlbumHot.DataBind();
        //}

        //private void loadCaSiVietNamTop15()
        //{
        //    reCaSiVietNam.DataSource = csbus.LayDanhSachCaSiVietNamTop15();
        //    reCaSiVietNam.DataBind();
        //}

        //private void loadCaSiAuMyTop15()
        //{
        //    reCaSiAuMy.DataSource = csbus.LayDanhSachCaSiAuMyTop15();
        //    reCaSiAuMy.DataBind();
        //}

        //private void loadCaSiHanQuocTop15()
        //{
        //    reCaSiHanQuoc.DataSource = csbus.LayDanhSachCaSiHanQuocTop15();
        //    reCaSiHanQuoc.DataBind();
        //}

        //private void loadBaiHatMoi()
        //{
        //    reBaiHatMoi.DataSource = bhbus.User_LoadDsTop20();
        //    reBaiHatMoi.DataBind();
        //}

        private void loadBXHTop20()
        {
            reBXHNhac.DataSource = bhbus.User_LoadBXHTop20();
            reBXHNhac.DataBind();
        }
        private void LoadBaiHatNoiBat()
        {
            reBaiHat.DataSource = bhbus.LayDanhSachBHNoiBat();
            reBaiHat.DataBind();
        }
        private void LoadAlbumHot()
        {
            reHotAlbum.DataSource = abbus.LayTop3AlbumHotNhat();
            reHotAlbum.DataBind();
        }
        private void LoadAlbumMoiNhat()
        {
            reAlbumMoiNhat.DataSource = abbus.LayTop6AlbumMoiNhat();
            reAlbumMoiNhat.DataBind();
        }
        private void LoadVideoMoiNhat()
        {
            reVideo.DataSource = videoBus.LayTop8VideoMoinhat();
            reVideo.DataBind();
        }

        private void LoadBXHVideo()
        {
            reBXHVideo.DataSource = videoBus.LayTop20VideoHaynhat();
            reBXHVideo.DataBind();
        }

        protected void reBaiHat_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            int MaBaiHat;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaBaiHat = Convert.ToInt16(usrCurrent["MaBaiHat"]);
                Repeater reDSCaSiBaiHat = (Repeater)e.Item.FindControl("reDSCaSiBaiHat");
                reDSCaSiBaiHat.DataSource = csbus.LayCaSiTheoBaiHatTable(MaBaiHat);
                reDSCaSiBaiHat.DataBind();
            }
        }

        protected void reVideo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            int MaVideo;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaVideo = Convert.ToInt16(usrCurrent["MaVideo"]);
                Repeater reDanhSachCaSiVideo = (Repeater)e.Item.FindControl("reDanhSachCaSiVideo");
                reDanhSachCaSiVideo.DataSource = videoBus.LayChiTietVideo(MaVideo);
                reDanhSachCaSiVideo.DataBind();
            }
        }

        protected void reBXHVideo_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            int MaVideo;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView usrCurrent = e.Item.DataItem as DataRowView;
                MaVideo = Convert.ToInt16(usrCurrent["MaVideo"]);
                Repeater reCaSiBXHVideo = (Repeater)e.Item.FindControl("reCaSiBXHVideo");
                reCaSiBXHVideo.DataSource = videoBus.LayChiTietVideo(MaVideo);
                reCaSiBXHVideo.DataBind();
            }
        }

        protected void reBaiHat_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="download")
            {
                string linkbai = e.CommandArgument.ToString();
                Response.ContentType = "audio/mp3";
                Response.AddHeader("content-disposition", "attachment; filename=" + linkbai);
                Response.TransmitFile(Server.MapPath("~/Nhac/" + linkbai));
                Response.End();
            }
        }
    }
}